#include <unistd.h>
#include <memory>
#include <stdarg.h>
#include <thread>
#include <string.h>
#include"api.h"
#include "third_party/spdlog/include/spdlog/sinks/daily_file_sink.h"
#include "third_party/spdlog/include/spdlog/sinks/rotating_file_sink.h"
#include "third_party/spdlog/include/spdlog/sinks/basic_file_sink.h" 
using namespace std;
int mycb(void * obj, asrMsg* pMsg, int uselss)
{
	printf("\n\nmycb Pointer %p\n\n\n",obj);
	int*  pInt = (int*)(obj);
	*pInt = 1; 
	printf("command is %d\n", pMsg->_iCommand);
	if(pMsg->_pAsrRes)
	{
		printf("asr res is%s\n",pMsg->_pAsrRes->_pRes);
		printf("BeginTime is %d ms\n",pMsg->_pAsrRes->_iBeginTime);
	}
}
void threadloop()
{

	int i = 0;
	const char* uuid="test123";
	vadOptions options;
	memset(&options,0,sizeof(vadOptions));
	options._iVadRenderId = RECO_WEBRTC;
	options._iVadRenderGiven =1;

	void* proxy = pCreateRecoProxy(uuid,strlen(uuid),&i,&options,NULL,mycb);
	FILE* pcmfd = fopen("./iat_pcm_8k_2.pcm", "rb");
    fseek(pcmfd, 0, SEEK_END);
    long wav_length = ftell(pcmfd);
    printf("wav file length = %ld Byte\n", wav_length);
    fseek(pcmfd, 0, SEEK_SET);
	char* buf = (char*)malloc(320);
	int iLen = fread(buf, 1, 320, pcmfd);
	printf("buf is %s, iLen=%d\n",buf,iLen);
	while (iLen > 0)
	{
		pFeedDataToRecoProxy(proxy,buf,iLen);
		iLen = fread(buf, 1, 320, pcmfd);
	}
	fclose(pcmfd);
	free(buf);
	pCloseRecoProxy(proxy);
}
int main()
{
	//shared_ptr<spdlog::logger>  m_log = spdlog::rotating_logger_mt("daily_logger2","/var/log/recoProxy2.log", 100, 100);;
	//shared_ptr<spdlog::logger> m_spLogger = spdlog::basic_logger_mt("asr_proxy_logger", "aa.log");
	//m_spLogger->info("OKOK");
	std::thread th1(threadloop);
	std::thread th2(threadloop);
	std::thread th3(threadloop);
	th1.join();
	th2.join();
	th3.join();
	
	//sleep(12);
#if 0
	FILE* pcmfd = fopen("./iat_pcm_8k_2.pcm", "rb");
	char* buf = (char*)malloc(640);
	int iLen = fread(buf, 640, 1, pcmfd);
	    
	while (iLen > 0)
    {
        pFeedDataToRecoProxy(proxy,buf,iLen*640);
        iLen = fread(buf, 640, 1, pcmfd);
    }
    fclose(pcmfd);
    free(buf);
	sleep(5);
	printf("Call close\n");
#endif
	//after get final res, then we close it
	return 0;
}
